package com.uenpay.im.invokeitems.contacts;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qyx.android.entity.GroupTalkEntity;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.serializations.GroupListSerializer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 获取我的群聊列表
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class MyGroupsInvokItem extends HttpInvokeItem {

	public class MyGroupsInvokItemResult {
		public int status;
		public String msg;
		public ArrayList<GroupTalkEntity> groups;
	}

	public MyGroupsInvokItemResult getOutput() {
		return (MyGroupsInvokItemResult) GetResultObject();
	}

	public MyGroupsInvokItem() {
		String url = APIConfiguration.getBaseUrl() + "Chat/Chat/getChatList?"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) throws Exception {
		MyGroupsInvokItemResult result = new MyGroupsInvokItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			if (json.has("data")) {
				JSONArray dataJson = json.optJSONArray("data");
				if (dataJson != null) {
					result.groups = GroupListSerializer
							.deserializeGroups(dataJson);
				}
			}

		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}
		return result;
	}

}
